package com.qf.shopping.controll.carControll;

import com.qf.shopping.pojo.car.Car;
import com.qf.shopping.pojo.car.CarMake;
import com.qf.shopping.pojo.car.CarModel;
import com.qf.shopping.service.carService.ICarMakeService;
import com.qf.shopping.service.carService.ICarModelService;
import com.qf.shopping.service.carService.ICarService;
import com.qf.shopping.vo.carVO.CarConstom;
import com.qf.shopping.vo.carVO.CarVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author Humy
 * @date 2019/4/22 19:40
 */
@Controller
@RequestMapping("/car")
public class CarControll {

    @Autowired
    private ICarService carService = null;

    @Autowired
    private ICarMakeService carMakeService = null;

    @Autowired
    private ICarModelService carModelService = null;

    /**
     * 车款页面加载
     */
    @GetMapping("/getAllCars")
    public String getAllCars(Model model) {

        //查询所有车款
        List<CarVO> carVos = carService.queryAllCars();
        //查询所有品牌
        List<CarMake> carMakes = carMakeService.queryAll();
        //查询所有车系
        List<CarModel> carModels = carModelService.queryAllCarModel();

        //把所有类型放入共享域
        //1：微型车  2：小型 3：紧凑型车 4：中型车 5：中大型车 6：豪华车 7：SUV 8：MPV 9：跑车 10：概念车 11：面包车 12：皮卡 13：轻客
        //14：客车 15：卡车 16：轻卡 17：重卡'
        ArrayList<String> levelName = new ArrayList<>();
        levelName.add("微型车");
        levelName.add("小型车");
        levelName.add("紧凑型车");
        levelName.add("中型车");
        levelName.add("中大型车");
        levelName.add("豪华车");
        levelName.add("SUV");
        levelName.add("MPV");
        levelName.add("跑车");
        levelName.add("概念车");
        levelName.add("面包车");
        levelName.add("皮卡");
        levelName.add("轻客");
        levelName.add("客车");
        levelName.add("卡车");
        levelName.add("轻卡");
        levelName.add("重卡");


        model.addAttribute("carVos", carVos);
        model.addAttribute("carMakes", carMakes);
        model.addAttribute("carModels",carModels);
        model.addAttribute("levelName", levelName);

        return "car/car/list";
    }

    /**
     * 根据条件查询
     */
    @GetMapping("/getCars")
    public String getCarsByVo(CarConstom carConstom, Model model) {

        CarVO carVO = new CarVO();
        carVO.setCarConstom(carConstom);

        List<CarVO> carVos = carService.queryCars(carVO);

        model.addAttribute("carVos", carVos);
        return "car/car/list";
    }

    /**
     * 根据id查询
     */
    @GetMapping("/getCar")
    @ResponseBody
    public CarVO getCarById(Long id){
        CarVO carVO = carService.queryCar(id);
        return carVO;
    }


    /**
     * 新增车型
     */
    @PostMapping("/addCar")
    public String addCar(CarConstom carConstom) {
        System.out.println("调用addCar之前。。。");
        CarVO carVO = new CarVO();
        carVO.setCarConstom(carConstom);
        carService.addCar(carVO);

        System.out.println("模态框调用了addCar方法。。。");
        return "redirect:/car/getAllCars";
    }

    /**
     * 修改之前先获取car对象
     *
     * @param id
     * @return
     */
    @GetMapping("/updateCar")
    public CarVO updateCar(@PathParam("id") Long id, Model model) {
        CarVO carVO = carService.queryCar(id);
        model.addAttribute("carVo",carVO );
        return carVO;
    }

    /**
     * 提交修改车型
     */
    @PostMapping("/updateCar")
    public String updateCar(CarVO carVO,Model model) {

        carService.updateCar(carVO);

        return "redirect:/car/getAllCars";
    }

    /**
     * 删除车型
     */
    @GetMapping("/deleteCar/{id}")
    public String deleteCar(@PathVariable("id")Long... ids) {
        carService.deleteCar(ids);
        return "redirect:/car/getAllCars";
    }



}
